﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Auditing</title>
  <keywords>
    <keyword translate="true">Auditing</keyword>
    <keyword translate="true">MVVM</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Auditing</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are lots of lightweight MVVM frameworks out there, which work great for the basics. However, if you are writing larger enterprise applications, notifying the UI of changed properties isn&apos;t enough. For example, did you think about Command Authentication? Or what about sensor emulation for Windows Phone 7 (that Microsoft don’t provide)?</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Why auditing</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are many reasons why auditing should be added to an application. Most developers only add auditing to the database, but below are several reasons to add auditing to the client as well:</text></para>
    <list id="2" type="ul" listtype="bullet" formatstring="·" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Logging (what user did what on specific moments)</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Gather statistics (which views (view models) are used most)</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">See what features of your software are being used by checking if anyone is actually invoking specific commands</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Measure performance (how long does it take to update specific properties or why is a specific view-model so slow?)</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">With the auditing capabilities of Catel, you can create and register custom auditors that can handled changes and events of view models. This way, you can gather a lot of statistics or any information that you want to gather about the user experience. Below is a list of events that can be handled:</text></para>
    <list id="3" type="ul" listtype="bullet" formatstring="·" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelCreating</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelCreated</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnPropertyChanging</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnPropertyChanged</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnCommandExecuting</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelSaving</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelSaved</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelCanceling</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelCanceled</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelClosing</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">OnViewModelClosed</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The developer has all the freedom to handle one or more methods in an auditor. Of course multiple auditors are possible as well.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Creating an auditor</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Creating a new auditor is very simple. Create a new class, derive from </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Auditing_AuditorBase" styleclass="Normal" translate="true">AuditorBase</link><text styleclass="Normal" translate="true"> and override the methods you are interested in. The class example tracks the event to a fake analytics framework.</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Logs all commands to a custom analytics service.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">class</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CommandAuditor : AuditorBase</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Analytics _analytics = </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Analytics();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Called when a command of a view model has just been executed.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;param name=&quot;viewModel&quot;&gt;The view model.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;param name=&quot;commandName&quot;&gt;Name of the command, which is the name of the command property.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;param name=&quot;command&quot;&gt;The command that has been executed.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;param name=&quot;commandParameter&quot;&gt;The command parameter.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">override</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> OnCommandExecuted(IViewModel viewModel, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> commandName, ICatelCommand command, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> commandParameter)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    {</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">        _analytics.TrackEvent(viewModel.GetType(), &quot;commandName&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Registering an auditor</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Registering a new auditor is extremely easy as you can see in the code below:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">AuditingManager.RegisterAuditor(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> CommandAuditor());</text></para>
  </body>
</topic>
